// src/api/cashFlowApi.js
import { apiClient } from './base'

// 分页查询出纳流水列表
export const getList = (params) => {
  console.log('cashFlowApi.getList 参数:', params)
  return apiClient.get('/cnls', { params })
}

// 创建出纳流水
export const create = (data) => 
  apiClient.post('/cnls', data)

// 根据ID查询出纳流水详情
export const getById = (id) => 
  apiClient.get(`/cnls/${id}`)

// 更新出纳流水
export const update = (id, data) => 
  apiClient.put(`/cnls/${id}`, data)

// 删除出纳流水
export const deleteById = (id) => 
  apiClient.delete(`/cnls/${id}`)

// 查询操作

// 根据收支项目ID查询
export const searchByIncomeExpense = (szxmId) => 
  apiClient.get(`/cnls/search/szxm/${szxmId}`)

// 根据银行账户ID查询
export const searchByBankAccount = (yhzhId) => 
  apiClient.get(`/cnls/search/yhzh/${yhzhId}`)

// 根据合同ID查询
export const searchByContract = (htId) => 
  apiClient.get(`/cnls/search/contract/${htId}`)

// 根据日期范围查询
export const searchByDateRange = (startDate, endDate) => 
  apiClient.get('/cnls/search/date-range', { params: { startDate, endDate } })

// 统计操作

// 获取统计信息
export const getStatistics = (params) => 
  apiClient.get('/cnls/statistics', { params })

// 获取银行账户统计信息
export const getAccountStatistics = (params) => 
  apiClient.get('/cnls/statistics/accounts', { params })

// 统计总收入
export const getIncomeStatistics = () => 
  apiClient.get('/cnls/statistics/income')

// 统计总支出
export const getExpenseStatistics = () => 
  apiClient.get('/cnls/statistics/expense')

// 根据日期范围统计收入
export const getIncomeStatisticsByRange = (startDate, endDate) => 
  apiClient.get('/cnls/statistics/income/range', { params: { startDate, endDate } })

// 根据日期范围统计支出
export const getExpenseStatisticsByRange = (startDate, endDate) => 
  apiClient.get('/cnls/statistics/expense/range', { params: { startDate, endDate } })

// 账户余额查询
export const getAccountBalance = (params) => 
  apiClient.get('/cnls/account-balance', { params }) 

// 导出Excel
export const exportData = (params) => 
  apiClient.get('/cnls/export', { 
    params,
    responseType: 'blob'
  })

// 验证收支日期
export const validateSzrq = (yhzhId, szrq) => 
  apiClient.get('/cnls/validate/szrq', { 
    params: { yhzhId, szrq } 
  })

// 默认导出
export default {
  getList,
  create,
  getById,
  update,
  delete: deleteById,
  searchByIncomeExpense,
  searchByBankAccount,
  searchByContract,
  searchByDateRange,
  getStatistics,
  getAccountStatistics,
  getIncomeStatistics,
  getExpenseStatistics,
  getIncomeStatisticsByRange,
  getExpenseStatisticsByRange,
  getAccountBalance,
  export: exportData,
  validateSzrq
} 